package com.s63.ssm.servlet;

import com.mysql.cj.util.StringUtils;
import com.s63.ssm.entity.Page;
import com.s63.ssm.entity.SysUser;
import com.s63.ssm.service.RoleService;
import com.s63.ssm.service.UserService;
import com.s63.ssm.utils.SpringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebServlet(urlPatterns = "/users")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        UserService userService = SpringUtils.getBean(UserService.class);
        RoleService roleService = SpringUtils.getBean(RoleService.class);

        String opr = req.getParameter("opr");
        if ("list".equals(opr)) {
            String realName = req.getParameter("realName");
            String roleId = req.getParameter("roleId");
            String pageNo = req.getParameter("pageNo");
            String pageSize = req.getParameter("pageSize");
            Page<SysUser> page = new Page<>();

            page.setPageNo(StringUtils.isNullOrEmpty(pageNo) ? 1 : Integer.valueOf(pageNo));
            page.setPageSize(StringUtils.isNullOrEmpty(pageSize) ? 2 : Integer.valueOf(pageSize));

            userService.getByRealNameAndRoleIdPage(realName, StringUtils.isNullOrEmpty(roleId) ? null : Integer.valueOf(roleId), page);
            req.setAttribute("page", page);
            req.setAttribute("realName", realName);
            req.setAttribute("roleId", roleId);
//            req.setAttribute("userList", userService.getAll());
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } else if ("toAdd".equals(opr)) {
            req.setAttribute("roleList", roleService.getAll());
            req.getRequestDispatcher("/add.jsp").forward(req, resp);
        } else if ("add".equals(opr)) {
            String account = req.getParameter("account");
            String realName = req.getParameter("realName");
            String roleId = req.getParameter("roleId");
            SysUser user = new SysUser();
            user.setAccount(account)
                    .setRealName(realName)
                    .setRoleId(Integer.valueOf(roleId));
            int count = userService.add(user);
            req.setAttribute("msg", count > 0 ? "添加成功" : "添加失败");
            req.getRequestDispatcher("/users?opr=list").forward(req, resp);
        } else if ("toUpdate".equals(opr)) {
            // 跳转到修改页面
            String id = req.getParameter("id");
            SysUser user = userService.getById(Integer.valueOf(id));
            req.setAttribute("user", user);
            req.setAttribute("roleList", roleService.getAll());
            req.getRequestDispatcher("/update.jsp").forward(req, resp);
        } else if ("update".equals(opr)) {
            //完成修改操作
            String account = req.getParameter("account");
            String realName = req.getParameter("realName");
            String roleId = req.getParameter("roleId");
            String id = req.getParameter("id");
            SysUser user = new SysUser();
            user.setAccount(account)
                    .setRealName(realName)
                    .setRoleId(Integer.valueOf(roleId)).setId(Integer.valueOf(id));
            int count = userService.update(user);
            req.setAttribute("msg", count > 0 ? "修改成功" : "修改失败");
            req.getRequestDispatcher("/users?opr=list").forward(req, resp);
        } else if ("del".equals(opr)) {
            int count = userService.del(Integer.valueOf(req.getParameter("id")));
            req.setAttribute("msg", count > 0 ? "删除成功" : "删除失败");
            req.getRequestDispatcher("/users?opr=list").forward(req, resp);
        } else {
            System.out.println("没有传递opr参数");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
